package com.ggh.jinjilive.ui.live;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.common.MerchantSession;
import com.ggh.jinjilive.mobile.ResultEntity;
import com.ggh.jinjilive.mobile.create.StartLiveMobile;
import com.ggh.jinjilive.mobile.home.live.LiveList;
import com.ggh.jinjilive.mobile.live.LiveMessageMobile;
import com.ggh.jinjilive.mobile.live.SellGoodsMobile;
import com.ggh.jinjilive.remotedata.InterceptObserver;
import com.ggh.jinjilive.remotedata.systemapi.SystemApiService;
import com.ggh.jinjilive.ui.BaseViewModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class AudienceLiveViewModel extends BaseViewModel {
    //房间信息
    public ObservableField<LiveList> liveList = new ObservableField<>();
    //直播间信息
    public MutableLiveData<LiveMessageMobile> mLiveMessageMobile = new MutableLiveData<>();
    //获取倒计时
    public MutableLiveData<Integer> mInteger = new MutableLiveData<>();
    //用户端正在带货的商品
    public MutableLiveData<SellGoodsMobile> mSellGoodsMobile = new MutableLiveData<>();

    public AudienceLiveViewModel(SystemApiService apiCallInterface) {
        super(apiCallInterface);

    }

    /**
     * 直播间信息
     */
    public void anchorView(String user_id){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id",user_id);
        paramsa.put("anchor_id",liveList.get().getUser_id());
        addSubscribe(apiCallInterface.anchorView(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LiveMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LiveMessageMobile liveMessageMobile = (LiveMessageMobile) resultEntity.getData();
                mLiveMessageMobile.setValue(liveMessageMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 获取倒计时
     */
    public void getServiceTime(){
        Map<String, Object> paramsa = new HashMap<>();
        addSubscribe(apiCallInterface.getServiceTime(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<Integer>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                Integer integer = (Integer) resultEntity.getData();
                if(integer > 0){
                    mInteger.setValue(integer);
                }
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }



    /**
     * 领取直播间红包雨
     */
    public void getRedsrain(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id", MerchantSession.getInstance().getUser().getUser_id());
        paramsa.put("live_id",liveList.get().getLive_id());
        addSubscribe(apiCallInterface.getRedsrain(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LiveMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LiveMessageMobile liveMessageMobile = (LiveMessageMobile) resultEntity.getData();
                ToastUtils.make().show("本次抢到了" + liveMessageMobile.getDiamond() + "钻石");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }

    /**
     * 领取平台红包雨
     */
    public void getRedsrains(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("user_id",MerchantSession.getInstance().getUser().getUser_id());
        paramsa.put("live_id",liveList.get().getLive_id());
        addSubscribe(apiCallInterface.getRedsrains(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<LiveMessageMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                LiveMessageMobile liveMessageMobile = (LiveMessageMobile) resultEntity.getData();
                ToastUtils.make().show("本次抢到了" + liveMessageMobile.getDiamond() + "钻石");
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }


    /**
     * 用户端正在带货的商品
     */
    public void userTaskInfo(){
        Map<String, Object> paramsa = new HashMap<>();
        paramsa.put("anchor_user_id",liveList.get().getUser_id());
        addSubscribe(apiCallInterface.userTaskInfo(paramsa).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new InterceptObserver<ResultEntity<SellGoodsMobile>>() {
            @Override
            public void onNextData(ResultEntity resultEntity) {
                SellGoodsMobile sellGoodsMobile = (SellGoodsMobile) resultEntity.getData();
                mSellGoodsMobile.setValue(sellGoodsMobile);
            }

            @Override
            public void onErrorNet(Throwable t, String netMsg) {

            }

        }));
    }
}

